package ru.spb.osll.myfit.client.widget;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.TextBox;

public class CellTextBox extends TextBox {

    private String rawText;
    private String parsedText;

    public CellTextBox() {
      setReadOnly(true);
      this.addValueChangeHandler(new ValueChangeHandler<String>() {
          @Override
          public void onValueChange(ValueChangeEvent<String> stringValueChangeEvent) {
              if(!isReadOnly())
                  setRawText(stringValueChangeEvent.getValue());
          }
      });
    }

    public CellTextBox(String rawText, String parsedText) {
      this();
      this.parsedText = parsedText;
      this.rawText = rawText;
    } 
        
    public void setReadOnly(boolean isReadOnly) {
      super.setReadOnly(isReadOnly);
      updateVisibleText();
    }

    private void updateVisibleText(){
      if(isReadOnly()){
        super.setText(parsedText); 
      }else{
        super.setText(rawText);
      }
    }

    public void setParsedText(String parsedText) {
      this.parsedText = parsedText;
      updateVisibleText();
    }

    public void setText(String text){
      rawText = text;
      super.setText(text);
    }

    public void setRawText(String rawText) {
      this.rawText = rawText;
      updateVisibleText();
    }

    public String getText(){
      if(!isReadOnly()){
    	 return super.getText();
      }
      return rawText;
    }

}